//
//  IWTabBar.m
//  ItcastWeibo
//
//  Created by apple on 14-5-5.
//  Copyright (c) 2014年 itcast. All rights reserved.
//

#import "ZZTabBar.h"
#import "XBTabBarButton.h"
#import "PJXBounceAnimation.h"
@interface ZZTabBar()
@property (nonatomic, strong) NSMutableArray *tabBarButtons;
@property (nonatomic, weak) XBTabBarButton *selectedButton;
@property (nonatomic, strong) XBTabBarButton *button;
@property (nonatomic, strong) PJXBounceAnimation *bounceAnimation;
@property (nonatomic, assign) BOOL fromVC;
@end

@implementation ZZTabBar

- (NSMutableArray *)tabBarButtons
{
    if (_tabBarButtons == nil) {
        _tabBarButtons = [NSMutableArray array];
    }
    return _tabBarButtons;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
         

    }
    return self;
}



- (void)goFoundWithNote:(NSNotification *)note
{
    self.fromVC = YES;
    if ([self.delegate respondsToSelector:@selector(tabBar:didSelectedButtonFrom:to:)]) {
        [self.delegate tabBar:self didSelectedButtonFrom:self.selectedButton.tag to:2];
        
    }
 
    [self layoutSubviews];
}

- (void)goHomeWithNote:(NSNotification *)note
{
    self.fromVC = NO;
    [self buttonClick:nil];
    [self layoutSubviews];
}


- (void)addTabBarButtonWithItem:(UITabBarItem *)item
{
    self.button = [[XBTabBarButton alloc] init];
     
    [self addSubview:self.button];  // 68 135 232

    
    [self.tabBarButtons addObject:self.button];

//     self.button.iconView = [[PJXIconView alloc] initWithIcon:self.button.imageView textLabel:nil];
//     self.bounceAnimation = [[PJXBounceAnimation alloc] init];
//     self.bounceAnimation.textSelectedColor = [UIColor blueColor];
//     self.bounceAnimation.iconSelectedColor = [UIColor blueColor];
//     self.button.animation = self.bounceAnimation;
//     self.fromVC = NO;

    self.button.item = item;

    [self.button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchDown];
    
 
}

 
- (void)buttonClick:(XBTabBarButton *)button
{

     
    if ([self.delegate respondsToSelector:@selector(tabBar:didSelectedButtonFrom:to:)]) {
        
        [self.delegate tabBar:self didSelectedButtonFrom:self.selectedButton.tag to:button.tag];
        
    }
    
    self.selectedButton.selected = NO;
    button.selected = YES;
    self.selectedButton = button;

//     dispatch_async(dispatch_get_main_queue(), ^{
//          [button playAnimation];
//     });

    
}
 

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat h = self.frame.size.height;
    CGFloat w = self.frame.size.width; 
    
    CGFloat buttonH = h;
    CGFloat buttonW = w / 4;
    CGFloat buttonY = 0;
    
    for (int index = 0; index < self.tabBarButtons.count; index++) {
        
        @autoreleasepool {
            
            self.button = self.tabBarButtons[index];
            CGFloat buttonX = index * buttonW;
            
            self.button.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH);
            self.button.tag = index;

           if (self.button.tag == 0) {
               [self buttonClick:self.button];
           }

        }
        
        
    }
}


@end
